package com.hqj.client.controller;

import com.hqj.client.entity.RoleMenu;
import com.hqj.client.service.RoleMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author 黄啟军
 * @version 1.0
 * @date 2020/5/16/016
 */
@Api(tags = "RoleMenuController")
@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {

    @Resource
    private RoleMenuService roleMenuService;
    /***
     * RoleMenu分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "RoleMenu分页查询",notes = "分页查询RoleMenu方法详情",tags = {"RoleMenuController"})
    @GetMapping(value = "/search/{page}/{size}" )
        public String findPage(@PathVariable  int page, @PathVariable  int size, Model model){
        //调用RoleMenuService实现分页查询RoleMenu
       List<RoleMenu> pageInfo = roleMenuService.findPage(page, size);
       model.addAttribute("msg",pageInfo);
        return "/page/BlogIndex";
    }
    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "RoleMenu根据ID删除",notes = "根据ID删除RoleMenu方法详情",tags = {"RoleMenuController"})
    @DeleteMapping(value = "delete/{id}" )
    public String delete(@PathVariable Long id,Model model){
        //调用RoleMenuService实现根据主键删除
        int i = roleMenuService.delete(id);
        if (i>0){
            model.addAttribute("delete","删除成功");
        }
        return  "/page/BlogIndex";
    }

    /***
     * 修改RoleMenu数据
     * @param roleMenu
     * @param id
     * @return
     */
    @ApiOperation(value = "RoleMenu根据ID修改",notes = "根据ID修改RoleMenu方法详情",tags = {"RoleMenuController"})
    @PutMapping(value="update/{id}")
    public String update(@RequestBody @ApiParam(name = "RoleMenu对象",value = "传入JSON数据",required = false) RoleMenu roleMenu,@PathVariable Long id,Model model){
        //设置主键值
        roleMenu.setRoleId(id);
        //调用RoleMenuService实现修改RoleMenu
        int i= roleMenuService.update(roleMenu);
        if (i>0){
            model.addAttribute("update","修改成功");
        }
        return  "/page/BlogIndex";
    }

    /***
     * 新增RoleMenu数据
     * @param roleMenu
     * @return
     */
    @ApiOperation(value = "RoleMenu添加",notes = "添加RoleMenu方法详情",tags = {"RoleMenuController"})
    @PostMapping("/insert")
    public String insert(@RequestBody  @ApiParam(name = "RoleMenu对象",value = "传入JSON数据",required = true) RoleMenu roleMenu,Model model){
        //调用RoleMenuService实现添加RoleMenu
        int i = roleMenuService.add(roleMenu);
        if (i>0){
            model.addAttribute("insert","增加成功");
        }
        return "/page/BlogIndex";
    }

    /***
     * 根据ID查询RoleMenu数据
     * @param id
     * @return
     */
    @ApiOperation(value = "RoleMenu根据ID查询",notes = "根据ID查询RoleMenu方法详情",tags = {"RoleMenuController"})
    @GetMapping("findById/{id}")
    public String findById(@PathVariable Long id,Model model){
        //调用RoleMenuService实现根据主键查询RoleMenu
        RoleMenu roleMenu = roleMenuService.findById(id);
        if (roleMenu!=null){
            model.addAttribute("findById",roleMenu);
        }
        return "/page/BlogIndex";
    }

    /***
     * 查询RoleMenu全部数据
     * @return
     */
    @ApiOperation(value = "查询所有RoleMenu",notes = "查询所RoleMenu有方法详情",tags = {"RoleMenuController"})
    @GetMapping("/findAll")
    public String findAll(Model model){
        //调用RoleMenuService实现查询所有RoleMenu
        List<RoleMenu> list = roleMenuService.findAll();
        if (list.isEmpty()){
            model.addAttribute("findAll","未查询到");
            return "/page/BlogIndex";
        }else {
            model.addAttribute("findAll",list);
            return "/page/BlogIndex";
        }
    }
}
